package com.zxx.xin.shorthand.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import com.zxx.xin.shorthand.R;

/**
 * Created by xin on 2018/3/15.
 */

public class MultiRowTextView extends View {
    private float mTextSize = 0;
    private String mText;
    private int mFontHeight;
    private Paint mTextPaint;
    private int mHeight;
    private int mWidth;
    private int mLineWidth;
    Paint.FontMetrics mFontMetrics;
    public MultiRowTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MultiRowTextView);
        mTextSize = typedArray.getDimension(R.styleable.MultiRowTextView_textSize, getResources().getDimension(R.dimen.default_multi_row_text_size));
        mText = typedArray.getString(R.styleable.MultiRowTextView_text);
        typedArray.recycle();
        mTextPaint = new Paint();
        mTextPaint.setTextSize(mTextSize);
        mTextPaint.setColor(Color.BLACK);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/jianshi_default.otf"));
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        measureHeight(heightMeasureSpec);
        measureWidth();
        setMeasuredDimension(mWidth, mHeight);
    }

    private void measureHeight(int heightMeasureSpec) {
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        measureFontHeight();
        int heightResult = 500;
        if (heightSpecMode == MeasureSpec.AT_MOST){
            heightResult = mFontHeight * mText.length();
            if (heightResult > heightSpecSize){
                mHeight = heightSpecSize;
            } else {
                mHeight = heightResult;
            }
        } else if (heightSpecMode == MeasureSpec.EXACTLY){
            mHeight = heightSpecSize;
        }
    }


    @Override
    protected void onDraw(Canvas canvas) {
        measureFontHeight();
        measureLineWidth();
        int xPos = mWidth - mLineWidth;
        int yPos = 0;
        for (int i = 0; i < mText.length(); i++) {
            if (mText.charAt(i) == '\n') {
                xPos -= mLineWidth;
                yPos = 0;
            } else {
                canvas.drawText(String.valueOf(mText.charAt(i)), xPos, yPos + mFontHeight / 2 - mFontMetrics.top / 2 - mFontMetrics.bottom / 2, mTextPaint);
                yPos += mFontHeight;
                if (yPos + mFontHeight > mHeight) {
                    yPos = 0;
                    xPos -= mLineWidth;
                }
            }
        }
    }

    private void measureWidth() {
        measureLineWidth();
        int lineCount = 0;
        int yPos = 0;
        for(int i = 0; i < mText.length(); i++){
            if (mText.charAt(i) == '\n'){
                lineCount++;
                yPos = 0;
            } else {
                yPos += mFontHeight;
                if (yPos > mHeight){
                    yPos = 0;
                    i--;
                    lineCount++;
                }

                if (i == mText.length() - 1){
                    lineCount++;
                }
            }
        }
        lineCount++;//额外多一行
        mWidth = lineCount * mLineWidth;
    }

    private void measureFontHeight(){
        mTextPaint.setTextSize(mTextSize);
        mFontMetrics = mTextPaint.getFontMetrics();
        mFontHeight = (int)(Math.ceil(mFontMetrics.descent - mFontMetrics.ascent) * 1.1 + 2);
    }

    private void measureLineWidth() {
        mTextPaint.setTextSize(mTextSize);
        float[] textWidth = new float[1];
        mTextPaint.getTextWidths("正", textWidth);
        float[] spaceWidth = new float[1];
        mTextPaint.getTextWidths(" ", spaceWidth);
        mLineWidth = (int)(Math.ceil((textWidth[0] + spaceWidth[0]) * 0.95) + 2);
    }

    public void setText(String text){
        mText = text;
        requestLayout();
        postInvalidate();
    }
}
